﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core;
using ZHDJ.Core.Caching;
using ZHDJ.Data;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.Edu;
using ZHDJ.Data.Models.Org;
using ZHDJ.Data.Models.Questionnaire;
using ZHDJ.WebPage;

namespace ZHDJ.Web
{
    public partial class Default2 : PortalPage
    {
        public struct DefaultPageCacheKey
        {
            public const string PhotoWallCacheKey = "PARTY_DefaultPage_PhotoWall_DATASOURCE";
            public const string RankMemberCacheKey = "PARTY_DefaultPage_RankMember_DATASOURCE";
            public const string RankBranchCacheKey = "PARTY_DefaultPage_ RankBranch_DATASOURCE";
            public const string RankBranchActivityCacheKey = "PARTY_DefaultPage_ RankBranchActivity_DATASOURCE";
            public const string RankBranchMassLearningCacheKey = "PARTY_DefaultPage_ RankBranchMassLearning_DATASOURCE";
        }

        protected string webLoginId = string.Empty;
        MemoryCacheManager CacheManager = new MemoryCacheManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurrentUser != null)
                    webLoginId = CurrentUser.LoginID;
                
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            //图片轮播
            DataTable articleImageSource = PartyArticle.GetPortalArticleImage();

            rpArticleImage.DataSource = articleImageSource;
            rpArticleImage.DataBind();
            rpArticleImage2.DataSource = articleImageSource;
            rpArticleImage2.DataBind();
            rpArticleImage3.DataSource = articleImageSource;
            rpArticleImage3.DataBind();

            //通知公告
            rpNotificationAnnouncement.DataSource = PartyArticle.GetArticleTitle("", "Announcement", "Basic", "limit 0,5");
            rpNotificationAnnouncement.DataBind();

            //网上学院通知公告
            rpOnlineSchoolNotice.DataSource = PartyArticle.GetArticleTitle("", "PartySchoolOnline", "OnlineSchool", "limit 0,13");
            rpOnlineSchoolNotice.DataBind();

            #region 信息快递
            //高层聚焦
            rpHeighLevelFocus.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.InformationNewsletter.ToString(), SysEnum.ArticleInformationNewsletter.HeighLevelFocus.ToString(), "limit 0,9").Tables[0];
            rpHeighLevelFocus.DataBind();

            //今日山海天
            rpTodayShanHaiTian.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.InformationNewsletter.ToString(), SysEnum.ArticleInformationNewsletter.TodayShanHaiTian.ToString(), "limit 0,9").Tables[0];
            rpTodayShanHaiTian.DataBind();

            //党建快递
            rpPartyNewsletter.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.InformationNewsletter.ToString(), SysEnum.ArticleInformationNewsletter.PartyNewsletter.ToString(), "limit 0,9").Tables[0];
            rpPartyNewsletter.DataBind();
            #endregion

            #region 基层党建
            //机关党建
            rpOfficePartyBuilding.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.BasicParty.ToString(), SysEnum.ArticleBasicParrty.OfficePartyBuilding.ToString(), "limit 0,9").Tables[0];
            rpOfficePartyBuilding.DataBind();

            //农村党建
            rpCountrysidePartyBuilding.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.BasicParty.ToString(), SysEnum.ArticleBasicParrty.CountrysidePartyBuilding.ToString(), "limit 0,8").Tables[0];
            rpCountrysidePartyBuilding.DataBind();

            //社区党建
            rpCommunityPartyBuilding.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.BasicParty.ToString(), SysEnum.ArticleBasicParrty.CommunityPartyBuilding.ToString(), "limit 0,8").Tables[0];
            rpCommunityPartyBuilding.DataBind();

            //两新组织
            rpTwoNews.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.BasicParty.ToString(), SysEnum.ArticleBasicParrty.TwoNews.ToString(), "limit 0,8").Tables[0];
            rpTwoNews.DataBind();

            //第一书记
            rpMajorSecretary.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.BasicParty.ToString(), SysEnum.ArticleBasicParrty.MajorSecretary.ToString(), "limit 0,8").Tables[0];
            rpMajorSecretary.DataBind();
            #endregion

            #region 三务公开
            //党务公开
            rpPartyAffairs.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.ThreeAffairOpen.ToString(), SysEnum.ArticleAffairOpen.PartyAffairs.ToString(), "limit 0,7").Tables[0];
            rpPartyAffairs.DataBind();

            //村务公开
            rpVillageAffairs.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.ThreeAffairOpen.ToString(), SysEnum.ArticleAffairOpen.VillageAffairs.ToString(), "limit 0,7").Tables[0];
            rpVillageAffairs.DataBind();

            //财务公开
            rpFinance.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.ThreeAffairOpen.ToString(), SysEnum.ArticleAffairOpen.Finance.ToString(), "limit 0,7").Tables[0];
            rpFinance.DataBind();

            //涉农政策
            rpAgriculturalPolicy.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.ThreeAffairOpen.ToString(), SysEnum.ArticleAffairOpen.AgriculturalPolicy.ToString(), "limit 0,7").Tables[0];
            rpAgriculturalPolicy.DataBind();

            //党务政务直通车
            rpPartyRemittance.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.ThreeAffairOpen.ToString(), SysEnum.ArticleAffairOpen.PartyRemittance.ToString(), "limit 0,7").Tables[0];
            rpPartyRemittance.DataBind();

            ////第一书记
            //rpFirstSecretary.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.ThreeAffairOpen.ToString(), SysEnum.ArticleAffairOpen.FirstSecretary.ToString(), "limit 0,8").Tables[0];
            //rpFirstSecretary.DataBind();
            #endregion

            #region 干部工作
            //选拔作用
            rpSelect.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.CadreWork.ToString(), SysEnum.ArticleCadreWork.Select.ToString(), "limit 0,8").Tables[0];
            rpSelect.DataBind();

            //活动动态
            rpDynamic.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.CadreWork.ToString(), SysEnum.ArticleCadreWork.Dynamic.ToString(), "limit 0,8").Tables[0];
            rpDynamic.DataBind();

            //警示教育
            rpCaution.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.CadreWork.ToString(), SysEnum.ArticleCadreWork.Caution.ToString(), "limit 0,8").Tables[0];
            rpCaution.DataBind();

            //意见建议
            rpSuggestion.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.CadreWork.ToString(), SysEnum.ArticleCadreWork.Suggestion.ToString(), "limit 0,8").Tables[0];
            rpSuggestion.DataBind();

            //常用下载
            rpDownload.DataSource = PartyArticle.GetMoreArticleTitle(8, 0, "", "CadreWork", SysEnum.ArticleCadreWork.Download.ToString(), true).Tables[0];
            rpDownload.DataBind();
            #endregion

            #region 人才天地
            //最新公告
            rpAnnouncement.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.TalentWorld.ToString(), SysEnum.ArticleTalentWorld.Announcement.ToString(), "limit 0,8").Tables[0];
            rpAnnouncement.DataBind();

            //人才活动
            rpActivity.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.TalentWorld.ToString(), SysEnum.ArticleTalentWorld.Activity.ToString(), "limit 0,8").Tables[0];
            rpActivity.DataBind();

            //扶持政策
            rpPolicy.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.TalentWorld.ToString(), SysEnum.ArticleTalentWorld.Policy.ToString(), "limit 0,8").Tables[0];
            rpPolicy.DataBind();

            //评选申报
            rpChoose.DataSource = PartyArticle.GetArticleTitle("", SysEnum.ArticleModule.TalentWorld.ToString(), SysEnum.ArticleTalentWorld.Choose.ToString(), "limit 0,8").Tables[0];
            rpChoose.DataBind();

            //视频排行
            rptPartyEduCourceClickClountRank.DataSource = PartyEduCourseware.GetEduCourceClickCountRankList();
            rptPartyEduCourceClickClountRank.DataBind();

            rptPartyEduCourceCreateTimeRank.DataSource = PartyEduCourseware.GetEduCourceCreateDateRankList();
            rptPartyEduCourceCreateTimeRank.DataBind();

            //留影墙
            //this.rptHonorHall.DataSource = CacheManager.Get<DataTable>(DefaultPageCacheKey.PhotoWallCacheKey, () =>
            //{
            //    return PartyPhotoWallImage.GetPortalPhotoWallList(10);
            //});

            this.rptHonorHall.DataSource = PartyPhotoWallImage.GetPortalPhotoWallList(10);
            this.rptHonorHall.DataBind();



            #endregion

            //组织排名
            rpRankBranch.DataSource = CacheManager.Get<DataSet>(DefaultPageCacheKey.RankBranchCacheKey, () =>
            {
                return PartyScoreHelper.GetAllRankBranch(0, 9);
            });
            rpRankBranch.DataBind();

            //党员排名
            rpRankMember.DataSource = CacheManager.Get<DataSet>(DefaultPageCacheKey.RankMemberCacheKey, () =>
            {
                return PartyScoreHelper.GetAllRankMember(0, 9);
            });
            rpRankMember.DataBind();

            //组织生活排名
            rpRankBranchActivity.DataSource = CacheManager.Get<DataSet>(DefaultPageCacheKey.RankBranchActivityCacheKey, () =>
            {
                return PartyScoreHelper.GetAllRankBranchActivities(0, 9);
            });
            rpRankBranchActivity.DataBind();

            //组织集中学习排名
            rptRankBranchMassLearning.DataSource = CacheManager.Get<DataSet>(DefaultPageCacheKey.RankBranchMassLearningCacheKey, () =>
            {
                return PartyScoreHelper.GetAllRankBranchMassLearning(0, 9);
            });
            rptRankBranchMassLearning.DataBind();

            //个人学习排名
            rptRankMemberAll.DataSource = CacheManager.Get<DataSet>(DefaultPageCacheKey.RankMemberCacheKey, () =>
            {
                return PartyScoreHelper.GetAllRankMember(0, 9);
            });
            rptRankMemberAll.DataBind();
        }

        private void LoadData()
        {
            //飘窗
            this.FloatWindow();
            hdHasQuestionnaire.Value = "0";
            if (IsLogin)
            {
                //问卷调查
                var data = PartyQuestionnaire.GetListByLoginID(CurrentUser.LoginID, CurrentUser.runSate);
                if (data.Rows.Count > 0)
                {
                    hdHasQuestionnaire.Value = "1";
                }
                //每日一题控制
                var current = DB.SystemParameters.Find("ADailyTopic");
                if (current != null)
                {
                    if (current.ParamValue == "1")
                    {
                        hdA_daily_topic_switch.Value = "1";
                    }
                    else
                    {
                        hdA_daily_topic_switch.Value = "2";
                    }
                }
                else
                {
                    hdA_daily_topic_switch.Value = "2";
                }
                //每日一题
                var sql = @"select * from dayrecored where CONVERT(varchar(100), GETDATE(), 23)=CONVERT(varchar(100), InputTime, 23) and UserCode='" + CurrentUser.IdentityCard + "' order by InputTime desc;";
                int iii = 1;
                try
                {
                    iii = SqlServerHelper.ExecuteDataTable(sql).Rows.Count;
                }
                catch (Exception)
                {

                }
                if (iii <= 0)
                {
                    hdA_daily_topic.Value = "1";
                }
            }
        }

        /// <summary>
        /// 截取标题
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string CutLongTitle(object obj, int length)
        {
            string title = Convert.ToString(obj);
            if (title.Length > length)
            {
                title = title.Substring(0, length) + "...";
            }
            return title;
        }

        #region 首页飘窗
        /// <summary>
        /// 飘窗
        /// </summary>
        public void FloatWindow()
        {
            DataTable dt = PartyFloatWindow.GetSuoyeData().Tables[0];
            StringBuilder sb = new StringBuilder();
            string temp = @" <div id='Div{0}' class='souyewindowsshow' style='z-index: 9999;' position='{4}'>
            <a href='{1}' {5} >
                <img src='{2}' style='border: 0;'
                    title='{3}' alt='{3}' /></a><br />
            <a id='A{0}' class='piaochuangClose' style='cursor: pointer; color: #818181'>关闭</a>
        </div>";
            foreach (DataRow dr in dt.Rows)
            {
                var herf = "PortalPageRank.aspx?ID=" + dr["ID"];
                if (Convert.ToString(dr["ContentType"]) == "1")
                {
                    herf = Convert.ToString(dr["ContentUrl"]);
                }
                var tag = "target='_blank'";
                if (herf == "" || herf.Trim() == "#")
                {
                    tag = "";
                    herf = "javascript:void(0);";
                }
                sb.AppendFormat(temp, dr["ID"], herf, UpFileBusiness.GetFileUrl(Convert.ToString(dr["ImgUrl"])), dr["Title"], dr["Position"], tag);
            }
            if (!string.IsNullOrEmpty(sb.ToString()))
            {
                this.litwindowsshow.Text = sb.ToString();
            }
        }
        #endregion
        public string webHost
        {
            get
            {
                return WebHost;
            }
        }
    }
}